package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

/*
 * This class is the GameFrame which is the main frame in the 
 * 4 in a row game. 
 * Extends the JFrame class.
 */
public class GameFrame extends JFrame {

	private GUIController guiController;
	private JLabel background = loadBackgroundImage();
	private JLabel currentPlayer = new JLabel();
	private GameBoard gameBoard;
	JPanel currentPlayerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
	/*
	 * GameFrame constructor.
	 * This constructor creates a frame
	 */

	public GameFrame(GUIController guiController) {
		this.guiController = guiController;
		createFrame();
		gameBoard = new GameBoard(guiController);
	}

	/*
	 * Creates the main frame, fixed size with 400X400
	 */
	private void createFrame() {
		setSize(400, 400);
		setResizable(false);
		setTitle("Four In A Row");
		add(background, BorderLayout.CENTER);
		createBottomPanel();
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	/*
	 * This method updates the current player in the frame.
	 */

	public void updateCurrentPlayer() {
		currentPlayer.setText("Current player: "
				  + guiController.getCurrentPlayer());
		currentPlayer.repaint();
	}
	/*
	 * This method creates the upper game panel in the game frame.
	 * Contains the player names and the current player.
	 */

	public void addCurrentPlayerPanel() {
		// Sets text to the label
		currentPlayer.setText("Current player: " + guiController.getCurrentPlayer());
		// Add the label to the panel.  
		currentPlayerPanel.add(currentPlayer);
		// Add the panel to the main frame.
		add(currentPlayerPanel, BorderLayout.NORTH);
		currentPlayerPanel.repaint();
	}
	/*
	 * This method replaces the current background to the 
	 * game board.
	 */

	public void addGameBoard() {
		remove(background);
		addCurrentPlayerPanel();
		add(gameBoard, BorderLayout.CENTER);
		pack();
		repaint();
	}

	public void repaintGameBoard() {
		gameBoard.updateBoard();
		guiController.updateCurrentPlayer();
	}

	/*
	 * This method creates the button panel,
	 * creates the Exit and New Game button with
	 * action listeners.
	 */
	private void createBottomPanel() {
		// Creates the panel and two buttons.
		JPanel panel = new JPanel();
		JButton jbnNewGame = new JButton("New Game");
		JButton jbnNetworkGame = new JButton("Network Game");
		JButton jbnExitGame = new JButton("Exit Game");
		// Creates action listeners to the buttons.
		jbnNewGame.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				guiController.showSetUpGameDialog();
				addGameBoard();
			}
		});
		jbnNetworkGame.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				guiController.showSetUpNetworkGameDialog();
				addGameBoard();
			}
		});
		jbnExitGame.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		// Adds the buttons to the panel.
		panel.add(jbnNewGame);
		panel.add(jbnNetworkGame);
		panel.add(jbnExitGame);
		add(panel, BorderLayout.SOUTH);
	}

	/*
	 * This method loads and returns the background image.
	 * Error dialog will be shown if no such background exists.
	 */
	private JLabel loadBackgroundImage() {
		BufferedImage myPicture = null;
		try {
			myPicture = ImageIO.read(
					  new File("src\\gui\\images\\title.png"));
			return new JLabel(new ImageIcon(myPicture));
		} catch (IOException ex) {
			guiController.showErrorDialog("Error while loading background");
		}
		return null;
		// change when we use .jar
		//return new JLabel(new ImageIcon(getClass().getResource("title.png")));
	}
}
